package gamePack;

import gamePack.entities.Enemy;
import gamePack.entities.EntityA;
import gamePack.entities.EntityB;
import gamePack.entities.Player;
import gamePack.gfx.ImageLoader;
import gamePack.gfx.ImageManager;
import gamePack.gfx.SpriteSheet;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.Random;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {

	private static final long serialVersionUID = 1L;
	private static final int WIDTH = 500, HEIGHT = 800, SCALE = 1,
			TILESIZE = 32;
	private static boolean running = false;

	private Player player;
	private Controller control;
	private Physics fisica;

	private LinkedList<EntityA> entA;
	private LinkedList<EntityB> entB;

	private LinkedList<EntityA> aBorrarA;
	private LinkedList<EntityB> aBorrarB;

	private int enemy_count = 5;
	private int enemy_killed = 0;
	private int score = 0;

	private Thread gameThread;

	private BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
			BufferedImage.TYPE_INT_RGB);
	private BufferedImage spriteSheet;
	private ImageManager im;

	private Random r = new Random();

	public void init() {

		requestFocus();

		ImageLoader loader = new ImageLoader();
		spriteSheet = loader.load("/AV-8 spritesheet.png");

		SpriteSheet ss = new SpriteSheet(spriteSheet);

		im = new ImageManager(ss, this);

		fisica = new Physics();
		control = new Controller(this);
		player = new Player(200, 400, this, im, control, fisica);

		control.createEnemy(enemy_count);

		aBorrarA = new LinkedList<EntityA>();
		aBorrarB = new LinkedList<EntityB>();

		entA = control.getEntityAList();
		entB = control.getEntityBList();

		addKeyListener(new KeyManager(this));

	}

	public void tick() {
		player.tick();
		control.tick();

		if (enemy_killed >= enemy_count) {
			enemy_count += 2;
			enemy_killed = 0;
			control.createEnemy(enemy_count);
		}
		limpiarDestruidos();
	}

	public void render() {
		BufferStrategy bs = this.getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(3);
			return;
		}
		Graphics g = bs.getDrawGraphics();
		Font fnt0 = new Font("arial", Font.BOLD, 20);
		g.setFont(fnt0);
		g.setColor(Color.white);
		// Dibujar aca
		g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
		g.drawString("Score: " + score, 10, 20);
		player.render(g);
		control.render(g);

		// Fin de dibujo
		g.dispose();
		bs.show();
	}

	public void crearEnemigo() {
		control.addEntity(new Enemy(r.nextInt(WIDTH - 32), -10, this, control,
				im, fisica));
	}

	public int getHeight() {
		return HEIGHT;
	}

	public int getWidth() {
		return WIDTH;
	}

	public static int getScale() {
		return SCALE;
	}

	public static int getTileSize() {
		return TILESIZE;
	}

	public LinkedList<EntityA> getEntA() {
		return entA;
	}

	public LinkedList<EntityB> getEntB() {
		return entB;
	}

	public Player getPlayer() {
		return player;
	}

	public BufferedImage getSpriteSheet() {
		return spriteSheet;
	}

	public Controller getControler() {
		return control;
	}

	public synchronized void start() {
		if (running)
			return;
		running = true;
		gameThread = new Thread(this);
		gameThread.start();
	}

	public synchronized void stop() {
		if (!running)
			return;
		running = false;
		try {
			gameThread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		init();
		long lastTime = System.nanoTime();
		final double amountOfTicks = 60D;
		double ns = 1000000000 / amountOfTicks;
		double delta = 0;

		while (running) {
			long now = System.nanoTime();
			delta = delta + (now - lastTime) / ns;
			lastTime = now;
			if (delta >= 1) {
				tick();
				delta--;
			}
			render();
		}
		stop();
	}

	public void aBorrarEntA(EntityA entA) {
		aBorrarA.add(entA);
	}

	public void aBorrarEntB(EntityB entB) {
		aBorrarB.add(entB);
	}

	public void limpiarDestruidos() {
		for (int i = 0; i < aBorrarA.size(); i++) {
			control.removeEntity(aBorrarA.get(i));
			;
		}

		for (int i = 0; i < aBorrarB.size(); i++) {
			control.removeEntity(aBorrarB.get(i));
			;
		}
	}

	public static void main(String[] args) {
		Game game = new Game();

		JFrame frame = new JFrame("Aerial");
		frame.setSize(WIDTH, HEIGHT);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setFocusable(true);
		frame.add(game);
		frame.setVisible(true);

		game.start();
	}

	public int getEnemy_count() {
		return enemy_count;
	}

	public void setEnemy_count(int enemy_count) {
		this.enemy_count = enemy_count;
	}

	public int getEnemy_killed() {
		return enemy_killed;
	}

	public void setEnemy_killed(int enemy_killed) {
		this.enemy_killed = enemy_killed;
	}

	public void aumentarScore(int p) {
		score = score + p;
	}
	

}
